package net.caixiaomi.info.ui.main;

import android.Manifest;
import android.content.Intent;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.baidu.mobstat.StatService;
import com.sobot.chat.SobotApi;
import com.sobot.chat.api.model.Information;

import net.caixiaomi.info.R;
import net.caixiaomi.info.WebViewActivity;
import net.caixiaomi.info.adapter.MineAdapter;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.BaseAlertDialog;
import net.caixiaomi.info.base.BaseFragment;
import net.caixiaomi.info.base.permissions.Permission;
import net.caixiaomi.info.base.permissions.RxPermissions;
import net.caixiaomi.info.base.quickadapter.BaseQuickAdapter;
import net.caixiaomi.info.helper.AppHelper;
import net.caixiaomi.info.model.BaseCallModel;
import net.caixiaomi.info.model.CommonEvent;
import net.caixiaomi.info.model.MineItem;
import net.caixiaomi.info.model.UserInfoModel;
import net.caixiaomi.info.net.RequestService;
import net.caixiaomi.info.net.ResponseCallback;
import net.caixiaomi.info.net.ResponseError;
import net.caixiaomi.info.net.RetrofitManage;
import net.caixiaomi.info.ui.about.AboutActivity;
import net.caixiaomi.info.ui.about.FeedbackActivity;
import net.caixiaomi.info.ui.coupons.CouponsActivity;
import net.caixiaomi.info.ui.details.AccountDetailsActivity;
import net.caixiaomi.info.ui.login.LoginActivity;
import net.caixiaomi.info.ui.message.MessageCenterActivity;
import net.caixiaomi.info.ui.mine.CollectActivity;
import net.caixiaomi.info.ui.mine.IdentifyActivity;
import net.caixiaomi.info.ui.mine.PersonalInfoActivity;
import net.caixiaomi.info.ui.mine.RechargeActivity;
import net.caixiaomi.info.ui.order.OrderRecordActivity;
import net.caixiaomi.info.ui.withdraw.WithdrawActivity;
import net.caixiaomi.info.util.Constants;
import net.caixiaomi.info.util.EventWhat;
import net.caixiaomi.info.util.GlideCacheUtil;
import net.caixiaomi.info.util.SchemeUtil;
import net.caixiaomi.info.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.functions.Consumer;
import retrofit2.Call;

/**
 * 用户中心
 */

public class UserFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener, View.OnClickListener {
    public static final String TAG = "UserFragment";

    @BindView(R.id.title)
    TextView mTitle;
    @BindView(android.R.id.list)
    RecyclerView mListView;

    private ImageView mImage;
    private TextView mWithdrawCash;//可提现余额
    private TextView mBalance;//账户余额
    private TextView mPhone;
    private View mNoIdentify;//未认证
    private View mIdentified;//已认证
    private View mHideView;

    private MineAdapter mAdapter;
    private boolean mInsertAction = false;
    private RxPermissions mRxPermissions;
    private List<MineItem> mActionList;

    public static UserFragment newInstance() {
        return new UserFragment();
    }

    @OnClick(R.id.btn_setting)
    void toSetting() {
        startActivity(new Intent(getActivity(), PersonalInfoActivity.class));
    }

    @Override
    protected int fragmentLayout() {
        return R.layout.frag_mine;
    }

    @Override
    protected void onFragmentCreated(View view, @Nullable Bundle savedInstanceState) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(getString(R.string.app_name)).append(" · ").append(getString(R.string.C_MINE));
        mTitle.setText(stringBuilder);
        initView();
    }

    private void initView() {
        try {

            View headerView = LayoutInflater.from(getActivity()).inflate(R.layout.header_mine, null);
            mImage = (ImageView) headerView.findViewById(R.id.image);
            mPhone = (TextView) headerView.findViewById(R.id.phone);
            mNoIdentify = headerView.findViewById(R.id.no_identify);
            mNoIdentify.setOnClickListener(this);
            mIdentified = headerView.findViewById(R.id.identify);
            mBalance = (TextView) headerView.findViewById(R.id.balance);
            mWithdrawCash = (TextView) headerView.findViewById(R.id.withdraw_cash);
            mHideView = headerView.findViewById(R.id.view1);
            headerView.findViewById(R.id.btn_recharge).setOnClickListener(this);
            headerView.findViewById(R.id.btn_withdraw).setOnClickListener(this);
            headerView.findViewById(R.id.image).setOnClickListener(this);


            TextView footView = new TextView(getActivity());
            footView.setId(R.id.btn_logout);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.topMargin = (int) getResources().getDimension(R.dimen.padding_16);
            layoutParams.bottomMargin = (int) getResources().getDimension(R.dimen.padding_16);
            layoutParams.leftMargin = (int) getResources().getDimension(R.dimen.padding_16);
            layoutParams.rightMargin = (int) getResources().getDimension(R.dimen.padding_16);
            footView.setLayoutParams(layoutParams);
            footView.setText(R.string.C_LOGOUT);
            footView.setBackgroundResource(R.drawable.btn_button_corner);
            footView.setTextColor(ContextCompat.getColor(CommonApp.getInstance(), R.color.white));
            footView.setPadding(0, (int) getResources().getDimension(R.dimen.padding_12), 0, (int) getResources().getDimension(R.dimen.padding_12));
            footView.setGravity(Gravity.CENTER);
            footView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.font_14));
            footView.setOnClickListener(this);

            List<MineItem> data = createData();

            mListView.setLayoutManager(new LinearLayoutManager(getActivity()));
            mAdapter = new MineAdapter(R.layout.item_mine, data);
            mAdapter.addHeaderView(headerView);
            mAdapter.addFooterView(footView);
            mAdapter.setOnItemClickListener(this);
            mListView.setAdapter(mAdapter);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private List<MineItem> createData() {
        boolean dealVersion = CommonApp.STORE.getBoolean(Constants.KEY_DEAL_VERSION, false);
        List<MineItem> data = new ArrayList<>();
        if (dealVersion) {
            data.add(new MineItem(R.id.btn_record, R.mipmap.ic_record, getString(R.string.C_PURCHASE_RECORDER), "", "", 0, 1, 0));
            data.add(new MineItem(R.id.btn_details, R.mipmap.ic_details, getString(R.string.C_ACCOUNT_DETAIL), "", "", 0, 1, 0));
            data.add(new MineItem(R.id.btn_coupons, R.mipmap.ic_coupons, getString(R.string.C_MINE_COUPONS), "", "", 0, 0, CommonApp.STORE.getBoolean(Constants.KEY_BONUS_NOTICE, false) ? 1 : 0));
            data.add(new MineItem(R.id.btn_message, R.mipmap.ic_message, getString(R.string.C_MINE_MESSAGE), "", "", (int) getResources().getDimension(R.dimen.padding_8), 1, CommonApp.STORE.getBoolean(Constants.KEY_MESSAGE_NOTICE, false) ? 1 : 0));
        }
        data.add(new MineItem(R.id.btn_collect, R.mipmap.ic_collect, getString(R.string.C_MINE_COLLECT), "", "", 0, 1, 0));
        data.add(new MineItem(R.id.btn_clear, R.mipmap.ic_clear, getString(R.string.C_CLEAR_CACHE), "", "", 0, dealVersion ? 1 : 0, 0));
        if (dealVersion) {
            data.add(new MineItem(R.id.btn_help, R.mipmap.ic_help, getString(R.string.C_HELP_CENTER), "", "", 0, 1, 0));
            data.add(new MineItem(R.id.btn_online_service, R.mipmap.ic_service_online, getString(R.string.C_CUSTOMER_ONLINE_SERVICE), "", "", dealVersion ? 0 : (int) getResources().getDimension(R.dimen.padding_8), 1, 0));
        }
        if (!dealVersion) {
            data.add(new MineItem(R.id.btn_service, R.mipmap.ic_service, getString(R.string.C_CUSTOMER_SERVICE), getString(R.string.C_CUSTOMER_SERVICE_PHONE), "", dealVersion ? 0 : (int) getResources().getDimension(R.dimen.padding_8), 1, 0));
            data.add(new MineItem(R.id.btn_feedback, R.mipmap.ic_feedback, getString(R.string.C_FEEDBACK), "", "", 0, 1, 0));
            data.add(new MineItem(R.id.btn_protocol, R.mipmap.ic_protocol, getString(R.string.C_REGISTER_PROTOCOL), "", "", 1, 1, 0));
        }
        data.add(new MineItem(R.id.btn_about, R.mipmap.ic_about, getString(R.string.C_ABOUT), "", "", 0, 0, 0));
        return data;
    }

    @Override
    public void onResume() {
        super.onResume();
        StatService.onPageStart(getActivity(), "wode-view");
        try {
            UserInfoModel userInfo = CommonApp.getUserInfo();
            if (userInfo != null) {
                updateUserState(userInfo);
                refreshData();
            }
            mListView.setVisibility(userInfo != null ? View.VISIBLE : View.GONE);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        StatService.onPageEnd(getActivity(), "wode-view");
    }

    private void updateUserState(UserInfoModel userInfo) {
        try {
            boolean dealVersion = CommonApp.STORE.getBoolean(Constants.KEY_DEAL_VERSION, false);
            mPhone.setText(userInfo.getMobile());
            if (dealVersion) {
                mNoIdentify.setVisibility(Integer.valueOf(userInfo.getIsReal()) == 0 ? View.VISIBLE : View.GONE);
                mIdentified.setVisibility(Integer.valueOf(userInfo.getIsReal()) != 0 ? View.VISIBLE : View.GONE);
            } else {
                mNoIdentify.setVisibility(View.GONE);
                mIdentified.setVisibility(View.GONE);
            }
            mHideView.setVisibility(dealVersion ? View.VISIBLE : View.GONE);


            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(TextUtils.isEmpty(userInfo.getTotalMoney()) ? "0.00" : userInfo.getTotalMoney());
            String money = stringBuilder.toString();
            stringBuilder.append(getString(R.string.C_MONEY));
            stringBuilder.append("\n").append(getString(R.string.C_ACCOUNT_BALANCE));
            SpannableString spannableString = new SpannableString(stringBuilder);
            spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.orange_primary)), 0, money.length() + 1, SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            spannableString.setSpan(new RelativeSizeSpan(1.6f), 0, money.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            spannableString.setSpan(new StyleSpan(Typeface.BOLD), 0, money.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            spannableString.setSpan(new RelativeSizeSpan(1.1f), money.length(), money.length() + 1, SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);

            mBalance.setText(spannableString);

            stringBuilder.setLength(0);
            stringBuilder.append(TextUtils.isEmpty(userInfo.getUserMoney()) ? "0.00" : userInfo.getUserMoney());
            money = stringBuilder.toString();
            stringBuilder.append(getString(R.string.C_MONEY));
            stringBuilder.append("\n").append(getString(R.string.C_WITHDRAW_CASH));
            spannableString = new SpannableString(stringBuilder);
            spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.orange_primary)), 0, money.length() + 1, SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            spannableString.setSpan(new RelativeSizeSpan(1.6f), 0, money.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            spannableString.setSpan(new StyleSpan(Typeface.BOLD), 0, money.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            spannableString.setSpan(new RelativeSizeSpan(1.1f), money.length(), money.length() + 1, SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);

            mWithdrawCash.setText(spannableString);

            if (dealVersion) {
                if (mInsertAction) {
                    if (mActionList.size() == userInfo.getActivityDTOList().size()) {
                        for (int index = 0; index < mActionList.size(); index++) {
                            MineItem item = mAdapter.getData().get(3 + index);
                            item.setActUrl(userInfo.getActivityDTOList().get(index).getActUrl());
                            item.setTitle(userInfo.getActivityDTOList().get(index).getTitle());
                        }
                    } else {
                        for (int index = 0; index < mActionList.size(); index++) {
                            mAdapter.getData().remove(3 + index);
                        }
                        mInsertAction = false;
                    }
                }
                mActionList = userInfo.getActivityDTOList();
                if (!mInsertAction && mActionList != null && !mActionList.isEmpty()) {
                    for (int index = 0; index < mActionList.size(); index++) {
                        MineItem mineItem = mActionList.get(index);
                        if (index == 0) {
                            mineItem.setMargin((int) getResources().getDimension(R.dimen.padding_8));
                        }
                        if (index == mActionList.size() - 1) {
                            mineItem.setVisableLine(0);
                        }
                        int resourceId = AppHelper.getResourceId("btn_action" + index);
                        if (resourceId != -1) {
                            mineItem.setId(resourceId);
                        }
                    }
                    mAdapter.getData().addAll(3, mActionList);
                    mInsertAction = true;
                }
                mAdapter.notifyDataSetChanged();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void refreshData() {
        JSONObject params = new JSONObject();
        params.put("time", String.valueOf(System.currentTimeMillis()));
        RequestService requestService = RetrofitManage.getInstance().getRequestService();
        Call<BaseCallModel<UserInfoModel>> userInfoExceptPass = requestService.userInfoExceptPass(params);
        userInfoExceptPass.enqueue(new ResponseCallback<BaseCallModel<UserInfoModel>>(getActivity()) {
            @Override
            public void onSuccess(BaseCallModel<UserInfoModel> data) {
                //更新userInfo数据
                UserInfoModel userInfo = data.data;
                userInfo.setToken(CommonApp.getToken());
                updateUserState(userInfo);
                userInfo.setActivityDTOList(null);
                CommonApp.setUserInfo(userInfo);

            }

            @Override
            public void onFail(ResponseError error) {

            }

            @Override
            public void onError() {
            }
        });
    }

    private void logout() {
        JSONObject params = new JSONObject();
        params.put("time", String.valueOf(System.currentTimeMillis()));
        RequestService requestService = RetrofitManage.getInstance().getRequestService();
        Call<BaseCallModel> logout = requestService.logout(params);
        logout.enqueue(new ResponseCallback<BaseCallModel>() {
            @Override
            public void onSuccess(BaseCallModel data) {
            }

            @Override
            public void onFail(ResponseError error) {

            }

            @Override
            public void onError() {
            }
        });
    }

    private void updateUnReadNotice(int type) {
        JSONObject params = new JSONObject();
        params.put("type", type);
        RequestService requestService = RetrofitManage.getInstance().getRequestService();
        Call<BaseCallModel> updateUnReadNotice = requestService.updateUnReadNotice(params);
        updateUnReadNotice.enqueue(new ResponseCallback<BaseCallModel>() {
            @Override
            public void onSuccess(BaseCallModel data) {

            }

            @Override
            public void onFail(ResponseError error) {

            }

            @Override
            public void onError() {
            }
        });
    }


    private void showCallDialog() {
        AppHelper.sendStatistics("lianxikefu");
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(getString(R.string.C_CALL)).append(" ").append(getString(R.string.C_CUSTOMER_SERVICE_PHONE));
        BaseAlertDialog dialog = new BaseAlertDialog(getActivity());
        dialog.setMessage(stringBuilder.toString());
        dialog.setPositiveButton(R.string.C_CALL, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppHelper.sendStatistics("lxkfhujiao");
                Intent intent = new Intent(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:" + getString(R.string.C_CUSTOMER_SERVICE_PHONE)));
                startActivity(intent);
            }
        });
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
        dialog.setNegativeButton(R.string.C_CANCEL, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppHelper.sendStatistics("lxkfquxiao");
            }
        });
        dialog.show();
    }

    private void showLogoutDialog() {
        AppHelper.sendStatistics("tuichudenglu");
        BaseAlertDialog dialog = new BaseAlertDialog(getActivity());
        dialog.setMessage(getString(R.string.C_LOGOUT_TIP));
        dialog.setPositiveButton(R.string.C_GOING_TO_LOGOUT, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                logout();
                EventBus.getDefault().post(new CommonEvent(EventWhat.EVENT_LOGIN_OUT_RETURN_HOME));
                CommonApp.removeUserInfo();
                mListView.scrollToPosition(0);
                toLogin();
            }
        });
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
        dialog.setNegativeButton(R.string.C_BACK, null);
        dialog.show();
    }

    private void showNoIdentifyDialog() {
        BaseAlertDialog dialog = new BaseAlertDialog(getActivity());
        dialog.setMessage(getString(R.string.C_NO_IDENTIFY_TIP));
        dialog.setPositiveButton(R.string.C_GOING_TO_IDENTIFY, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toIdentify();
            }
        });
        dialog.setNegativeButton(R.string.C_I_KNOW, null);
        dialog.show();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        try {
            switch (view.getId()) {
                case R.id.btn_record:
                    toBettingRecord();
                    break;
                case R.id.btn_details:
                    toAccountDetails();
                    break;
                case R.id.btn_coupons:
                    toMineCoupon();
                    break;
                case R.id.btn_message:
                    toMineMessage();
                    break;
                case R.id.btn_help:
                    toHelpCenter();
                    break;
                case R.id.btn_service:
                    toCustomerService();
                    break;
                case R.id.btn_online_service:
                    toOnlineService();
                    break;
                case R.id.btn_about:
                    toAbout();
                    break;
                case R.id.btn_collect:
                    toCollect();
                    break;
                case R.id.btn_feedback:
                    toFeedback();
                    break;
                case R.id.btn_clear:
                    toClearCache();
                    break;
                case R.id.btn_protocol:
                    toRegistProtocol();
                    break;

                case R.id.btn_action0:
                case R.id.btn_action1:
                case R.id.btn_action2:
                    toAction(position);
                    break;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 注册服务协议
     */
    private void toRegistProtocol() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(Constants.BASE_H5_URL).append(getString(R.string.H5_REGISTER_SERVICE));
        String url = stringBuilder.toString();
        Intent intent = new Intent(getActivity(), WebViewActivity.class);
        intent.putExtra("url", url);
        startActivity(intent);
    }

    /**
     * 清除缓存
     */
    private void toClearCache() {
        GlideCacheUtil.getInstance().clearImageAllCache();
        ToastUtil.s(R.string.C_CLEAR_CACHE_DONE);
    }


    /**
     * 投诉建议
     */
    private void toFeedback() {
        AppHelper.sendStatistics("tousukefu");
        startActivity(new Intent(getActivity(), FeedbackActivity.class));
    }


    /**
     * 推广活动
     */
    private void toAction(int position) {
        try {
            MineItem mineItem = mAdapter.getData().get(position);
            SchemeUtil.parseIntentUrl(getActivity(), mineItem.getActUrl(), "");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        try {
            switch (v.getId()) {
                case R.id.btn_logout:
                    showLogoutDialog();
                    break;
                case R.id.no_identify:
                    toIdentify();
                    break;
                case R.id.btn_recharge:
                    toRecharge();
                    break;
                case R.id.btn_withdraw:
                    toWithdraw();
                    break;
                case R.id.image:
//                    toSetting();
                    break;

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 充值
     */
    private void toRecharge() {
        AppHelper.sendStatistics("chongzhi");
        Intent intent = new Intent(getActivity(), RechargeActivity.class);
        startActivity(intent);
    }

    /**
     * 提现
     */
    private void toWithdraw() {
        UserInfoModel userInfo = CommonApp.getUserInfo();
        if (!TextUtils.equals(userInfo.getIsReal(), "1")) {//提现先实名制
            showNoIdentifyDialog();
            return;
        }
        AppHelper.sendStatistics("tixian");
        Intent intent = new Intent(getActivity(), WithdrawActivity.class);
        startActivity(intent);
    }


    /**
     * 投注记录
     */
    private void toBettingRecord() {
        AppHelper.sendStatistics("touzhujilu");
        Intent intent = new Intent(getActivity(), OrderRecordActivity.class);
        startActivity(intent);

    }

    /**
     * 账户明细
     */
    private void toAccountDetails() {
        AppHelper.sendStatistics("zhanghumingxi");
        Intent intent = new Intent(getActivity(), AccountDetailsActivity.class);
        startActivity(intent);
    }

    /**
     * 我的卡券
     */
    private void toMineCoupon() {
        AppHelper.sendStatistics("wodekaquan");
        boolean bonusNotice = CommonApp.STORE.getBoolean(Constants.KEY_BONUS_NOTICE, false);
        if (bonusNotice) {
            CommonApp.STORE.set(Constants.KEY_BONUS_NOTICE, false);
            EventBus.getDefault().post(new CommonEvent(EventWhat.EVENT_NEW_NOTICE_POINT));
            updateUnReadNotice(1);
        }
        Intent intent = new Intent(getActivity(), CouponsActivity.class);
        startActivity(intent);

    }

    /**
     * 我的消息
     */
    private void toMineMessage() {
        AppHelper.sendStatistics("xiaoxizhongxin");
        boolean messageNotice = CommonApp.STORE.getBoolean(Constants.KEY_MESSAGE_NOTICE, false);
        if (messageNotice) {
            CommonApp.STORE.set(Constants.KEY_MESSAGE_NOTICE, false);
            EventBus.getDefault().post(new CommonEvent(EventWhat.EVENT_NEW_NOTICE_POINT));
            updateUnReadNotice(2);
        }
        Intent intent = new Intent(getActivity(), MessageCenterActivity.class);
        startActivity(intent);
    }

    /**
     * 帮助中心
     */
    private void toHelpCenter() {
        AppHelper.sendStatistics("bangzhuzhongxin");
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(Constants.BASE_H5_URL).append(getString(R.string.H5_HELP_CENTER));
        String url = stringBuilder.toString();
        Intent intent = new Intent(getActivity(), WebViewActivity.class);
        intent.putExtra("url", url);
        intent.putExtra("eventId", "bangzhuzhongxin-view");
        startActivity(intent);
    }

    /**
     * 联系客服
     */
    private void toCustomerService() {
        if (mRxPermissions == null) {
            mRxPermissions = new RxPermissions(getActivity());
        }
        mRxPermissions
                .requestEach(
                        Manifest.permission.CALL_PHONE
                )
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) throws Exception {
                        if (permission.granted) {
                            showCallDialog();
                        } else if (permission.shouldShowRequestPermissionRationale) {
                            // 用户拒绝了该权限，没有选中『不再询问』（Never ask again）,那么下次再次启动时，还会提示请求权限的对话框
                        } else {
                            // 用户拒绝了该权限，并且选中『不再询问』
                        }
                    }
                });
    }

    /**
     * 联系在线客服
     */
    private void toOnlineService() {
        Information info = new Information();
        info.setAppkey(getString(R.string.SOBOT_KEY));
        SobotApi.startSobotChat(getActivity(), info);
    }

    /**
     * 关于我们
     */
    private void toAbout() {
        AppHelper.sendStatistics("guanyuwomen");
        Intent intent = new Intent(getActivity(), AboutActivity.class);
        startActivity(intent);
    }

    /**
     * 实名认证
     */
    private void toIdentify() {
        AppHelper.sendStatistics("shimingrenzheng");
        Intent intent = new Intent(getActivity(), IdentifyActivity.class);
        startActivity(intent);
    }

    private void toLogin() {
        Intent intent = new Intent(getActivity(), LoginActivity.class);
        startActivity(intent);
    }

    /**
     * 我的收藏
     */
    private void toCollect() {
        AppHelper.sendStatistics("wodeshoucang");
        Intent intent = new Intent(getActivity(), CollectActivity.class);
        startActivity(intent);
    }


    @Override
    public void onPostEvent(CommonEvent event) {
        switch (event.getWhat()) {
            case EventWhat.EVENT_APP_BACKGROUND_UPDATE_DEAL_VERSION:
                mAdapter.getData().clear();
                List<MineItem> data = createData();
                mAdapter.addData(data);
                break;
            case EventWhat.EVENT_NEW_NOTICE_POINT:
                for (MineItem item : mAdapter.getData()) {
                    if (item.getId() == R.id.btn_coupons) {
                        item.setVisablePoint(CommonApp.STORE.getBoolean(Constants.KEY_BONUS_NOTICE, false) ? 1 : 0);
                    }
                    if (item.getId() == R.id.btn_message) {
                        item.setVisablePoint(CommonApp.STORE.getBoolean(Constants.KEY_MESSAGE_NOTICE, false) ? 1 : 0);
                    }
                }
                mAdapter.notifyDataSetChanged();
                break;
        }
    }

}
